import cn.hutool.core.util.ObjectUtil;
import com.github.kiulian.downloader.YoutubeDownloader;
import com.github.kiulian.downloader.downloader.client.ClientType;
import com.github.kiulian.downloader.downloader.request.RequestVideoFileDownload;
import com.github.kiulian.downloader.downloader.request.RequestVideoInfo;
import com.github.kiulian.downloader.downloader.response.Response;
import com.github.kiulian.downloader.model.videos.VideoInfo;
import com.github.kiulian.downloader.model.videos.formats.Format;

import java.awt.*;
import java.io.File;

public class DownloadVideo {
    public static void main(String[] args) throws Throwable {

        YoutubeDownloader downloader = new YoutubeDownloader();
        Format format = getFormat(downloader);
        File outDir = new File("videos");
        String fileName = "myAwesomeName";

        Response<File> responseFile = downloader.downloadVideoFile(new RequestVideoFileDownload(format).saveTo(outDir));

        File file = responseFile.data();
        System.out.println(file.getAbsoluteFile());
    }

    private static Format getFormat(YoutubeDownloader downloader) throws Throwable {
        RequestVideoInfo request = new RequestVideoInfo("6R74gsdeJX0");
        ClientType original = ClientType.ANDROID_TV;
        request.clientType(original);
        request.proxy("127.0.0.1", 7897);
        Response<VideoInfo> response = downloader.getVideoInfo(request);
        Throwable error = response.error();
        if(ObjectUtil.isNotNull(error)){
            throw error;
        }
        VideoInfo video = response.data();

        int itag = 18;
        return video.findFormatByItag(itag);
    }

}